#ifdef __MMI_DSM_NEW__

#include "MMI_Include.h"
#include "CommonScreens.h"


#include "mrp_include.h"

#if defined(__MMI_SKYQQ__) && !defined(WIN32) 
#include "sky_qq_open.h"
#endif

#define DSM_EDIT_BUF_MAX                              512
#define DSM_TEXT_LEN                                       (1024*2)
#define DSM_DIALOG_LEN                                   (512)
#define DSM_TITLE_LEN                                      50
#define DSM_STACK_SIZE                                    20
#define IMG_DSM_MAIN_MENU_IMAGE               (GRP_ID_MYTHROAD - 1)


//adaptor
#if (MTK_VERSION >= 0x0824)
#define INPUT_TYPE_PHONE_NUMBER						IMM_INPUT_TYPE_NUMERIC
#define INPUT_TYPE_ALPHANUMERIC_PASSWORD			IMM_INPUT_TYPE_ALPHANUMERIC_PASSWORD
#define INPUT_TYPE_ALPHANUMERIC_SENTENCECASE			IMM_INPUT_TYPE_SENTENCE
#endif


typedef enum
{
    MR_LIST_NO_ICON = 0,
    MR_LIST_NORMAL_ICON = 1,
    MR_LIST_SINGLE_SEL = 2,
    MR_LIST_MUTI_SEL = 3,
    MR_LIST_TYPE_MAX
}E_DSM_LISTTYPE;


typedef enum
{
	 DSM_TEXT,
	 DSM_EDIT,
	 DSM_MENU,
	 DSM_DIALOG,
	 DSM_WIN,
	 DSM_WEATHER
}T_DSM_WIN_TYPE;


typedef struct 
{
	 int32 CurrSrcID;
	 char title[DSM_TITLE_LEN];
	 T_DSM_WIN_TYPE type;
}T_DSM_NODE;


typedef struct 
{
	T_DSM_NODE node;
	int32 texttype;
	char *data;
}T_DSM_TEXT;


typedef struct 
{
	T_DSM_NODE node;
	int32 edittype;
	int32 max;
	char *data;
	char *pTmp;
}T_DSM_EDIT;


typedef struct 
{
	T_DSM_NODE node;
	int32 dialogtype;
	char *data;
}T_DSM_DIALOG;


typedef struct 
{ 
	T_DSM_NODE node;
	int16 hilight;
	int16 num;
	char *item[MAX_SUB_MENUS];
	char select[MAX_SUB_MENUS];
	int16 listType;
}T_DSM_MENU;


MR_BOOL  internalCovered = MR_FALSE;

static U8 dsmScreenId[DSM_STACK_SIZE] = {0};
static T_DSM_NODE * dsmStack[DSM_STACK_SIZE] = {0};
static int32 dsmCurScrIndex = 0;
static U16 *pDsmTitle = NULL;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

extern void UI_common_screen_exit(void);
extern void UI_common_screen_pre_exit(void);
static void DSM_ExecuteCurrHiliteHandler(S32 hiliteindex);
static void mr_menu_back(void);
static void mr_menu_entry_next(void);
static void freeNodeMem(T_DSM_NODE *pNode);
static void redraw_text(void);
static void redraw_edit(void);


int32 mr_localui_initialize(void)
{
	dsmCurScrIndex = -1;
	memset(dsmStack,0,sizeof(dsmStack));
}


int32 mr_localui_terminate(void)
{
	memset(dsmStack,0,sizeof(dsmStack));
}


void mr_localui_free_all_screen(void)
{
	int i;
	
	for (i = 0; i < DSM_STACK_SIZE;i++)
	{
		freeNodeMem(dsmStack[i]);
	}

	memset(dsmStack,0,sizeof(dsmStack));
	dsmCurScrIndex = -1;
}


int32 dsmAllocScrId(void)
{
     int32 i=0;

     for(i =0;i <DSM_STACK_SIZE;i++)
     {
            if(dsmScreenId[i] == 0)
            {
                   dsmScreenId[i] = 1;
	            return (DSM_SCREEN_BASE+i+1);
            }
     }
	 
     return -1;     
}


static void dsmFreeScrId(int32 scrid)
{
       int32 i = scrid - DSM_SCREEN_BASE -1;

	if(i>=0&&i < DSM_STACK_SIZE)
	{
		dsmScreenId[i] = 0;
	}
}


void dsmFreeScrIdAll(void)
{
	int32 i= 0;
	for(i = 0;i<DSM_STACK_SIZE;i++)
	{
		dsmScreenId[i] = 0;
	}
}


/****************************************************************************
函数名:T_DSM_NODE *findNodeByScrnID(int32 scrid)
描  述:在dsm自己维护的窗口栈中查找自动窗口id的窗口数据
参  数:scrid:指定要查找的窗口id
返  回:如果找到则返回窗口数据接口的指针，如果没有找到则放回null
****************************************************************************/
static T_DSM_NODE *findNodeByScrnID(int32 scrid)
{
      int i = 0;
	  
      for(i = dsmCurScrIndex;i>=0 ;i--)
      {
             if (dsmStack[i]->CurrSrcID == scrid)
	           return dsmStack[i];
      }
	  
      return NULL;
}


/****************************************************************************
函数名:static int findNodeIndex(int32 nID)
描  述:根据窗口 ID 在dsm自己维护的窗口栈中查找在栈中的位置
参  数:nID:指定要查找的窗口id
返  回:如果找到则返回窗口索引，如果没有找到则放回-1
****************************************************************************/
static int findNodeIndex(int32 nID)
{
      int i = 0;
	  
      for(i = dsmCurScrIndex;i>=0 ;i--)
      {
             if (dsmStack[i]->CurrSrcID == nID)
	           return i;
      }
	  
      return -1;
}


/****************************************************************************
函数名:static void freeNodeMem(T_DSM_NODE *pNode)
描  述:释放vm 创建的外部ui 申请的资源
参  数:ui 的数据结构指针
返  回:无
****************************************************************************/
static void freeNodeMem(T_DSM_NODE *pNode)
{
	T_DSM_TEXT *pText = NULL;
	T_DSM_DIALOG *pDialog = NULL;
	T_DSM_EDIT *pEdit = NULL;
	T_DSM_MENU *pMenu = NULL;
	int j = 0;
	
	if(pNode == NULL)
		return;
	else
	{
		if(pNode->type == DSM_DIALOG)
		{
			pDialog = (T_DSM_DIALOG *)pNode;
			OslMfree(pDialog->data);
			OslMfree(pDialog);
		}
		else if(pNode->type == DSM_TEXT)
		{
			pText= (T_DSM_TEXT*)pNode;
			OslMfree(pText->data);
			OslMfree(pText);
		}
		else if(pNode->type == DSM_EDIT)
		{
			pEdit= (T_DSM_EDIT*) pNode;
			OslMfree(pEdit->data);
			OslMfree(pEdit->pTmp);
			OslMfree(pEdit);
		}
		else if(pNode->type == DSM_MENU)
		{
			pMenu = (T_DSM_MENU *)pNode;
			
			for(j = 0;j< pMenu->num;j++)
				OslMfree(pMenu->item[j]);
			OslMfree(pMenu);
		}
		else
		{
			 OslMfree(pNode);
		}
	}
}


/****************************************************************************
函数名:static void deleteOneNode(int32 nId)
描  述:根据窗口id 释放一个vm 创建的ui控件
参  数:nId 窗口id
返  回:无
****************************************************************************/
static void deleteOneNode(int32 nId)
{
	T_DSM_NODE *pNode = dsmStack[dsmCurScrIndex];
       int i = 0,index = 0;

	dsmFreeScrId(nId);

	if(pNode->CurrSrcID == nId)
	{
		freeNodeMem(pNode);
		dsmStack[dsmCurScrIndex--] = NULL;
		GoBackHistory();
	}
	else
	{
		index = findNodeIndex(nId);
		
		if((index != -1)&&(index < dsmCurScrIndex))
		{
			freeNodeMem(dsmStack[index]);
			
			for(i = index;i < dsmCurScrIndex;i++)
		 	{
		 	      dsmStack[i] = dsmStack[i+1];
		 	}

			dsmStack[dsmCurScrIndex--] = NULL;
			
			//DeleteScreenIfPresent(nId);
			mmi_frm_scrn_close(mr_app_get_group_id(), nId);
		}
	}
}


int32 mr_menuCreateEx(const char * title, int16 num,int16 listtype)
{
	 T_DSM_MENU* pMenu = (T_DSM_MENU *)OslMalloc(sizeof(T_DSM_MENU));
	 int32 len = 0;
	 int32 slen=0;
	 
	 if (pMenu != NULL)
	 {
	       memset(pMenu,0,sizeof(T_DSM_MENU));
		pMenu->hilight = 0;
		pMenu->node.CurrSrcID = dsmAllocScrId();
		
		if(pMenu->node.CurrSrcID == -1)
		{
			OslMfree(pMenu);
			return MR_FAILED;
		}
		
		pMenu->node.type = DSM_MENU;

		if(num > MAX_SUB_MENUS)
			pMenu->num = MAX_SUB_MENUS;
		else
			pMenu->num = num;
		
		pMenu->listType = listtype;
		
		memset(pMenu->select,0,sizeof(pMenu->select));

		slen = mr_str_wstrlen((char *)title);
		len = slen>(DSM_TITLE_LEN-2)?(DSM_TITLE_LEN-2):slen;
		memcpy(pMenu->node.title,title,len);
		
		mr_str_convert_endian(pMenu->node.title);
		dsmCurScrIndex++;
		dsmStack[dsmCurScrIndex] = (T_DSM_NODE *)pMenu;
		return pMenu->node.CurrSrcID;
	 }
	  return MR_FAILED;
}


int32 mr_menuCreate(const char * title, int16 num)
{
	return mr_menuCreateEx(title, num, MR_LIST_NORMAL_ICON);
}


static void mr_platShowMenu(T_DSM_MENU *pMenu,U8*guiBuffer)
{
	switch(pMenu->listType)
	{
		case MR_LIST_SINGLE_SEL:
			ShowCategory109Screen(0, IMG_DSM_MAIN_MENU_IMAGE,
									  STR_GLOBAL_OK, IMG_GLOBAL_OK, 
									  STR_GLOBAL_BACK, IMG_GLOBAL_BACK,
									  (S32)pMenu->num, (U8**)pMenu->item, NULL,
									  (S32)pMenu->hilight, guiBuffer); 
			break;
		case MR_LIST_MUTI_SEL:
			ShowCategory140Screen (0, IMG_DSM_MAIN_MENU_IMAGE,
									(S32)pMenu->num,  (U8**)pMenu->item, 
									(U8*)pMenu->select, (S32)pMenu->hilight,
									guiBuffer);
			break;
		default :
			if (guiBuffer != NULL)
			{
				list_menu_category_history* hist = (list_menu_category_history*)guiBuffer;
				hist->highlighted_item = pMenu->hilight;
			}		
			ShowCategory353Screen((U8 *)pMenu->node.title, IMG_DSM_MAIN_MENU_IMAGE,
									STR_GLOBAL_OK, IMG_GLOBAL_OK,
									STR_GLOBAL_BACK, IMG_GLOBAL_BACK,
									(S32)pMenu->num,(U8**)pMenu->item,
									(U16*)gIndexIconsImageList,NULL,0,(S32)pMenu->hilight, guiBuffer);
		break;
	}	
}


int32 mr_menuSetFocus(int32 menu, int32 index)
{
	U8 *guiBuffer;
	T_DSM_MENU *pMenu = (T_DSM_MENU *)(dsmStack[dsmCurScrIndex]);

	if( pMenu != NULL)
	{
		if (pMenu->node.CurrSrcID == menu)
		{
			if(pMenu->num>index)
			{
				pMenu->hilight = index;

				guiBuffer = mmi_frm_scrn_get_active_gui_buf();	//GetCurrGuiBuffer(pMenu->node.CurrSrcID);
				RegisterHighlightHandler(DSM_ExecuteCurrHiliteHandler);
				pDsmTitle = (U16*)pMenu->node.title;
				mr_platShowMenu(pMenu,guiBuffer);
				pDsmTitle = NULL;
				SetRightSoftkeyFunction(mr_menu_back,KEY_EVENT_UP);
				SetLeftSoftkeyFunction(mr_menu_entry_next,KEY_EVENT_UP);
				//huangsunbo 20101227
				//SetCenterSoftkeyFunction(mr_menu_entry_next, KEY_EVENT_UP);
				SetKeyHandler(mr_menu_entry_next, KEY_CSK, KEY_EVENT_UP);
				ChangeCenterSoftkey(NULL, IMG_GLOBAL_COMMON_CSK);
				return MR_SUCCESS;
			}
		}
	}
	return MR_FAILED;
}


int32 mr_menuSetItemEx(int32 menu, const char *text, int32 index,int32 select)
{
        int len = 0;
        T_DSM_MENU *pMenu = (T_DSM_MENU *)findNodeByScrnID(menu);
	 
	 if(pMenu != NULL)
	 {
	       if ((index < MAX_SUB_MENUS)&&(index < pMenu->num))
	       {
	            len = mr_str_wstrlen((char *)text)+2;
				   
		     if(pMenu->item[index] != NULL)
		     {
		             OslMfree(pMenu->item[index]);
		     }
			 
			pMenu->item[index] = (char *)OslMalloc(len);
			pMenu->select[index] = (U16)select;
			
			if(pMenu->listType == MR_LIST_SINGLE_SEL)
			{
				if(pMenu->select[index] == 1)
					pMenu->hilight = index;
			}
			
			if( pMenu->item[index] != NULL)
			{
				memset(pMenu->item[index],0,len);
				memcpy(pMenu->item[index],text,len-2);
				mr_str_convert_endian(pMenu->item[index]);
				return MR_SUCCESS;
			}
	 	}
	 }
	 return MR_FAILED;
}


int32 mr_menuSetItem(int32 menu, const char *text, int32 index)
{
	 return mr_menuSetItemEx(menu,text,index,0);
}


/****************************************************************************
函数名:void DSM_ExecuteCurrHiliteHandler(S32 hiliteindex)
描  述:菜单focus到一个菜单时候的对调函数
参  数:hiliteindex:新focus的菜单项的索引
返  回:无
****************************************************************************/
static void DSM_ExecuteCurrHiliteHandler(S32 hiliteindex)
{     
	T_DSM_MENU *pMenu = (T_DSM_MENU *)(dsmStack[dsmCurScrIndex]);
	
	if (pMenu->node.type == DSM_MENU)
	{
		pMenu->hilight = hiliteindex;
	}
}


/****************************************************************************
函数名:void mr_menu_back(void)
描  述:按右软件时候的回调函数
参  数:无
返  回:无
****************************************************************************/
static void mr_menu_back(void)
{	
	mr_app_send_event_wrapper(MR_MENU_RETURN, 0, 0);	
}


/****************************************************************************
函数名:void mr_menu_entry_next(void)
描  述:选中一个菜单项的回调函数
参  数:无
返  回:无
****************************************************************************/
static void mr_menu_entry_next(void)
{
	T_DSM_MENU *pMenu = (T_DSM_MENU *)(dsmStack[dsmCurScrIndex]);

	if(pMenu->node.type == DSM_MENU)
		mr_app_send_event_wrapper(MR_MENU_SELECT,pMenu->hilight, 0);
}


/****************************************************************************
函数名:void real_show_menu(void)
描  述:绘制菜单界面
参  数:无
返  回:无
****************************************************************************/
static void real_show_menu(void)
{
      U8 *guiBuffer;
      T_DSM_MENU *pMenu = (T_DSM_MENU *)(dsmStack[dsmCurScrIndex]);

	// 第一次进入的时候和再次进入都要判断
	if (pMenu->node.type != DSM_MENU)
	{
		pMenu = (T_DSM_MENU*)findNodeByScrnID(mmi_frm_scrn_get_active_id());
		if (pMenu == NULL)
			return;
	}

      internalCovered = MR_TRUE;

      mr_app_EntryNewScreen(pMenu->node.CurrSrcID, mr_app_exit_dsm_screen, real_show_menu, MMI_FRM_FULL_SCRN);
	  
      internalCovered = MR_FALSE;

	mr_app_resume();

/* this feature add for __MMI_DSM_NEW_JSKY__, later will effect all */
#ifdef __MR_CFG_FEATURE_AUTO_ZOOMIN_ZOOMOUT__
	mr_layer_zoom_pause_ex();
#endif
	 
      guiBuffer = mmi_frm_scrn_get_active_gui_buf();	//GetCurrGuiBuffer(pMenu->node.CurrSrcID);
      RegisterHighlightHandler(DSM_ExecuteCurrHiliteHandler);
      pDsmTitle = (U16 *)pMenu->node.title;
	mr_platShowMenu(pMenu,guiBuffer);
	pDsmTitle = NULL;
  	SetRightSoftkeyFunction(mr_menu_back,KEY_EVENT_UP);
  	SetLeftSoftkeyFunction(mr_menu_entry_next,KEY_EVENT_UP);
	//huangsunbo 20101227
	SetCenterSoftkeyFunction(mr_menu_entry_next, KEY_EVENT_UP);
	ChangeCenterSoftkey(NULL, IMG_GLOBAL_COMMON_CSK);
}


int32 mr_menuShow(int32 menu)
{
      real_show_menu();
      return MR_SUCCESS;
}


int32 mr_menuRefresh(int32 menu)
{
	U8 *guiBuffer = NULL;
	T_DSM_MENU *pMenu = (T_DSM_MENU *)(dsmStack[dsmCurScrIndex]);

	if( pMenu != NULL)
	{
		if (pMenu->node.CurrSrcID == menu)
		{
			guiBuffer = mmi_frm_scrn_get_active_gui_buf();	//GetCurrGuiBuffer(pMenu->node.CurrSrcID);
			
			
			RegisterHighlightHandler(DSM_ExecuteCurrHiliteHandler);
			pDsmTitle = (U16 *)pMenu->node.title;
			mr_platShowMenu(pMenu,guiBuffer);
			pDsmTitle = NULL;
			SetRightSoftkeyFunction(mr_menu_back,KEY_EVENT_UP);
			SetLeftSoftkeyFunction(mr_menu_entry_next,KEY_EVENT_UP);
			//huangsunbo 20101227
			SetCenterSoftkeyFunction(mr_menu_entry_next, KEY_EVENT_UP);
			ChangeCenterSoftkey(NULL, IMG_GLOBAL_COMMON_CSK);
			return MR_SUCCESS;
		}
	}
	return MR_FAILED;
}


/****************************************************************************
函数名:static void mr_eventOk(void)
描  述:编辑框确认键回调函数
参  数:无
返  回:无
****************************************************************************/
static void mr_eventOk(void)
{
	mr_app_send_event_wrapper(MR_DIALOG_EVENT, MR_DIALOG_KEY_OK, 0);
}


/****************************************************************************
函数名:static void mr_eventCancel(void)
描  述:取消编辑的按键回调函数
参  数:无
返  回:无
****************************************************************************/
static void mr_eventCancel(void)
{
	mr_app_send_event_wrapper(MR_DIALOG_EVENT, MR_DIALOG_KEY_CANCEL, 0);

}


int32 mr_menuRelease(int32 menu)
{
	deleteOneNode(menu);
	return MR_SUCCESS;
}


static mr_platShowDialog(T_DSM_DIALOG *pDialog,U8*guiBuffer)
{
	switch(pDialog->dialogtype)
	{
		case MR_DIALOG_CANCEL:
		{
			U16 dsmKeyGrpoup[] = 
			{
				DSM_DEAL_KEYS
			};
			ShowCategory165Screen(NULL, NULL, STR_GLOBAL_BACK, IMG_GLOBAL_BACK,(UI_string_type)(pDialog->data),IMG_GLOBAL_INFO,guiBuffer);
			SetGroupKeyHandler(mr_app_dsm_screen_key_hdlr,dsmKeyGrpoup, sizeof(dsmKeyGrpoup) / sizeof(U16), KEY_EVENT_DOWN);
			SetGroupKeyHandler(mr_app_dsm_screen_key_hdlr,dsmKeyGrpoup, sizeof(dsmKeyGrpoup) / sizeof(U16), KEY_EVENT_UP);

			#if defined(__MR_CFG_FEATURE_OVERSEA__)  && defined( __MR_CFG_ENTRY_NES__)
			mr_app_setup_dsm_screen_key_hdlr();//for nes key settting
			#endif
			
			SetRightSoftkeyFunction(mr_eventCancel,KEY_EVENT_UP);
			SetRightSoftkeyFunction(NULL,KEY_EVENT_DOWN);
			break;
		}
		case MR_DIALOG_OK_CANCEL:
			ShowCategory165Screen(STR_GLOBAL_OK, IMG_GLOBAL_OK, STR_GLOBAL_BACK, IMG_GLOBAL_BACK,(UI_string_type)(pDialog->data),IMG_GLOBAL_QUESTION,guiBuffer);
			SetRightSoftkeyFunction(mr_eventCancel,KEY_EVENT_UP);
			SetLeftSoftkeyFunction(mr_eventOk,KEY_EVENT_UP);
			//huangsunbo 20101227
			SetCenterSoftkeyFunction(mr_eventOk, KEY_EVENT_UP);
			ChangeCenterSoftkey(NULL, IMG_GLOBAL_COMMON_CSK);
			break;
		case MR_DIALOG_NONE:
			ShowCategory165Screen(NULL, NULL, NULL, NULL,(UI_string_type)(pDialog->data),IMG_GLOBAL_INFO,guiBuffer);
			break;
		default:
			ShowCategory165Screen(STR_GLOBAL_OK, IMG_GLOBAL_OK, NULL, NULL,(UI_string_type)(pDialog->data),IMG_GLOBAL_INFO,guiBuffer);
			SetLeftSoftkeyFunction(mr_eventOk,KEY_EVENT_UP);
			//huangsunbo 20101227
			SetCenterSoftkeyFunction(mr_eventOk, KEY_EVENT_UP);
			ChangeCenterSoftkey(NULL, IMG_GLOBAL_COMMON_CSK);
			break;
	}	
}


/****************************************************************************
函数名:void EntryDsmDialog(void)
描  述:mmi架构调用的显示dialog的函数
参  数:无
返  回:无
****************************************************************************/
static void EntryDsmDialog(void)
{
	U8 *guiBuffer = NULL;
	//pBOOL bkIsBackHistory;
/* this feature add for __MMI_DSM_NEW_JSKY__, later will effect all */
#ifdef __MR_CFG_FEATURE_AUTO_ZOOMIN_ZOOMOUT__
	pBOOL need_pause_ex = FALSE;
#endif
	//extern pBOOL IsBackHistory;
	
	T_DSM_DIALOG *pDialog = (T_DSM_DIALOG *)(dsmStack[dsmCurScrIndex]);


	if (pDialog!= NULL)
	{
	     if(pDialog->node.type == DSM_DIALOG)
	     {
			if(dsmCurScrIndex > 0)
			{
				//bkIsBackHistory = IsBackHistory;
				//IsBackHistory = MMI_TRUE;
				dsmCurScrIndex--;
#if 0	// 太神秘了
				redraw_text();
				redraw_edit();
				real_show_menu();
#endif				
#ifdef __MMI_DSM_WEATHER_APP__		
				redraw_weather();
#endif				
				dsmCurScrIndex++;
				//IsBackHistory = bkIsBackHistory;
			/* this feature add for __MMI_DSM_NEW_JSKY__, later will effect all */
			#ifdef __MR_CFG_FEATURE_AUTO_ZOOMIN_ZOOMOUT__
				need_pause_ex = MMI_TRUE;
			#endif
			}	
	     }
	     else
	     {
			pDialog = (T_DSM_DIALOG*)findNodeByScrnID(mmi_frm_scrn_get_active_id());
			if (pDialog == NULL)
				return;	     
	     }
	}

	internalCovered = MR_TRUE;

	guiBuffer = mmi_frm_scrn_get_active_gui_buf();	//GetCurrGuiBuffer(pDialog->node.CurrSrcID);
	mr_app_EntryNewScreen(pDialog->node.CurrSrcID,mr_app_exit_dsm_screen,EntryDsmDialog, MMI_FRM_SMALL_SCRN);
	internalCovered = MR_FALSE;

	mr_app_resume();

	mr_platShowDialog(pDialog,guiBuffer);
}


int32 mr_dialogCreate(const char * title, const char * text, int32 type)
{
	T_DSM_DIALOG *pDialog = (T_DSM_DIALOG *)OslMalloc(sizeof(T_DSM_DIALOG));
	int32 len =0;
	int32 slen=0;
	
	if (pDialog!= NULL)
	{
		memset(pDialog,0,sizeof(T_DSM_DIALOG));
		pDialog->data = (char *)OslMalloc(DSM_DIALOG_LEN);
		
		if (pDialog->data != NULL)
		{
			memset(pDialog->data,0,DSM_DIALOG_LEN);
			pDialog->dialogtype = type;
			pDialog->node.CurrSrcID = dsmAllocScrId();

			if(pDialog->node.CurrSrcID == -1)
			{
			       OslMfree(pDialog->data);
				OslMfree(pDialog);
				return MR_FAILED;
			}
			
			pDialog->node.type = DSM_DIALOG;

			slen = mr_str_wstrlen((char *)text);
			len = slen >(DSM_DIALOG_LEN-2)?(DSM_DIALOG_LEN-2):slen;
			memcpy(pDialog->data,text,len);
			
			mr_str_convert_endian(pDialog->data);
			dsmCurScrIndex++;
			dsmStack[dsmCurScrIndex] = (T_DSM_NODE *)pDialog;
			EntryDsmDialog();
			return pDialog->node.CurrSrcID;
		}
		OslMfree(pDialog);
	}   
	return MR_FAILED;
}


int32 mr_dialogRelease(int32 dialog)
{
       deleteOneNode(dialog);
	return MR_SUCCESS;
}


int32 mr_dialogRefresh(int32 dialog, const char * title, const char * text, int32 type)
{
	U8 *guiBuffer = NULL;
	int32 len =0;
	int32 slen=0;
	T_DSM_DIALOG *pDialog = (T_DSM_DIALOG *)(dsmStack[dsmCurScrIndex]);
	
	if (pDialog!= NULL)
	{
		if(pDialog->node.CurrSrcID == dialog)
		{
			memset(pDialog->data,0,DSM_DIALOG_LEN);
			slen = mr_str_wstrlen((char *)text);
			len = slen >(DSM_DIALOG_LEN-2)?(DSM_DIALOG_LEN-2):slen;
			memcpy(pDialog->data,text,len);

			mr_str_convert_endian(pDialog->data);

			if(type != -1)
	                     pDialog->dialogtype = type;
					 
			guiBuffer = mmi_frm_scrn_get_active_gui_buf();	//GetCurrGuiBuffer(pDialog->node.CurrSrcID);
			
                     UI_common_screen_pre_exit();
			UI_common_screen_exit();
			mr_platShowDialog(pDialog,guiBuffer);
			return MR_SUCCESS;
		}
	}
	return MR_FAILED;
}


static void mr_platShowText(T_DSM_TEXT *pText,U8*guiBuffer)
{
	S32 iBufLength = mmi_ucs2strlen(pText->data) * sizeof(U8);

	switch(pText->texttype)
	{
	case MR_DIALOG_OK:
		ShowCategory74Screen(0, 0,STR_GLOBAL_OK, IMG_GLOBAL_OK,NULL, NULL,
									(U8*) pText->data, iBufLength,guiBuffer );   
		ChangeRightSoftkey(NULL,NULL);
		SetLeftSoftkeyFunction(mr_eventOk,KEY_EVENT_UP);
		//huangsunbo 20101227
		SetCenterSoftkeyFunction(mr_eventOk, KEY_EVENT_UP);
		ChangeCenterSoftkey(NULL, IMG_GLOBAL_COMMON_CSK);
		break;
	case MR_DIALOG_OK_CANCEL:
		ShowCategory74Screen(0, 0,STR_GLOBAL_OK, IMG_GLOBAL_OK,
									STR_GLOBAL_BACK, IMG_GLOBAL_BACK,
									(U8*) pText->data, iBufLength,guiBuffer );   
		SetRightSoftkeyFunction(mr_eventCancel,KEY_EVENT_UP);
		SetLeftSoftkeyFunction(mr_eventOk,KEY_EVENT_UP);
		//huangsunbo 20101227
		SetCenterSoftkeyFunction(mr_eventOk, KEY_EVENT_UP);
		ChangeCenterSoftkey(NULL, IMG_GLOBAL_COMMON_CSK);
		break;
	default:
		ShowCategory74Screen(0, 0,NULL, NULL,
									STR_GLOBAL_BACK, IMG_GLOBAL_BACK,
									(U8*) pText->data, iBufLength,guiBuffer );  
		ChangeLeftSoftkey(NULL,NULL);
		SetRightSoftkeyFunction(mr_eventCancel,KEY_EVENT_UP);
		break;
	}	
}


int32 mr_textCreate(const char * title, const char * text, int32 type)
{
       T_DSM_TEXT * pText = (T_DSM_TEXT *)OslMalloc(sizeof(T_DSM_TEXT));
	int32 len =0;
	int32 slen=0;
	
	 if(pText != NULL)
	 {
	      memset(pText,0,sizeof(T_DSM_TEXT));
		  
	      pText->data = (char *)OslMalloc(DSM_TEXT_LEN);
		  
	      if (pText->data!= NULL)
	      {
	             memset(pText->data,0,DSM_TEXT_LEN);
		      pText->node.CurrSrcID = dsmAllocScrId();

		      if(pText->node.CurrSrcID == -1)
		      {
		             OslMfree(pText->data);
			      OslMfree(pText);
			      return MR_FAILED;
		      }
			  
		      pText->node.type = DSM_TEXT;
		      pText->texttype = type;
		      dsmCurScrIndex++;
		      dsmStack[dsmCurScrIndex] = (T_DSM_NODE *)pText;

		      slen = mr_str_wstrlen((char *)title);
		      len = slen>(DSM_TITLE_LEN-2)?(DSM_TITLE_LEN-2):slen;
		      memcpy(pText->node.title,title,len);

		      slen = mr_str_wstrlen((char *)text);
		      len = slen>(DSM_TEXT_LEN-2)?(DSM_TEXT_LEN-2):slen;
		      memcpy(pText->data,text,len);
			  
		      mr_str_convert_endian(pText->node.title);
		      mr_str_convert_endian(pText->data);
	             redraw_text();
		      return pText->node.CurrSrcID;
	      	}
		OslMfree(pText);
	}
       return MR_FAILED;
}


int32 mr_textRefresh(int32 textid, const char * title, const char * text)
{
	U8* gui_buffer;	
	int32 len =0;
	int32 slen=0;
	
	T_DSM_TEXT * pText = (T_DSM_TEXT *)(dsmStack[dsmCurScrIndex]);

	if(pText != NULL)
	{
		if(pText->node.CurrSrcID == textid)
		{
			memset(pText->node.title,0,DSM_TITLE_LEN);
			memset(pText->data,0,DSM_TEXT_LEN);
			
			//memcpy(pText->node.title,title,mr_str_wstrlen((char *)title));
			//memcpy(pText->data,text,mr_str_wstrlen((char *) text));

			slen = mr_str_wstrlen((char *)title);
		       len = slen>(DSM_TITLE_LEN-2)?(DSM_TITLE_LEN-2):slen;
		       memcpy(pText->node.title,title,len);

		       slen = mr_str_wstrlen((char *)text);
		       len = slen>(DSM_TEXT_LEN-2)?(DSM_TEXT_LEN-2):slen;
		       memcpy(pText->data,text,len);

			  
			mr_str_convert_endian(pText->node.title);
			mr_str_convert_endian(pText->data);

			gui_buffer = mmi_frm_scrn_get_gui_buf(mr_app_get_group_id(), pText->node.CurrSrcID);	//GetCurrGuiBuffer(pText->node.CurrSrcID);
			
			// 不加下边两句会把虚拟键盘显示出来
			UI_common_screen_pre_exit();
			UI_common_screen_exit();
			
			pDsmTitle = (U16 *)pText->node.title;
			mr_platShowText(pText,gui_buffer);
			pDsmTitle = NULL;
			return MR_SUCCESS;
		}
	}
	return MR_FAILED;
}


/****************************************************************************
函数名:void redraw_text(void)
描  述:MMI结构调用的显示文本框的函数
参  数:无
返  回:无
****************************************************************************/
static void redraw_text(void)
{
	U8* gui_buffer;	
	S32 iBufLength = 0;
	T_DSM_TEXT * pText = (T_DSM_TEXT *)(dsmStack[dsmCurScrIndex]);

	if (pText->node.type != DSM_TEXT)
	{
		pText = (T_DSM_TEXT*)findNodeByScrnID(mmi_frm_scrn_get_active_id());
		if (pText == NULL)
			return;				
	}
	
	internalCovered = MR_TRUE;
	
       iBufLength = mmi_ucs2strlen(pText->data) * sizeof(U8);
	gui_buffer = mmi_frm_scrn_get_gui_buf(mr_app_get_group_id(), pText->node.CurrSrcID);	//GetCurrGuiBuffer(pText->node.CurrSrcID);

	mr_app_EntryNewScreen(pText->node.CurrSrcID, mr_app_exit_dsm_screen, redraw_text, MMI_FRM_FULL_SCRN );
	
	 internalCovered = MR_FALSE;
	 
	mr_app_resume();

/* this feature add for __MMI_DSM_NEW_JSKY__, later will effect all */
#ifdef __MR_CFG_FEATURE_AUTO_ZOOMIN_ZOOMOUT__
	mr_layer_zoom_pause_ex();
#endif
	
	pDsmTitle = (U16 *)pText->node.title;
	mr_platShowText(pText,gui_buffer);
	pDsmTitle = NULL;
}


int32 mr_textRelease(int32 text)
{
	deleteOneNode(text);
	return MR_SUCCESS;
}

#if (MTK_VERSION <= 0x0812)
#define IMM_INPUT_TYPE_ENGLISH_SENTENCE  INPUT_TYPE_USE_ONLY_ENGLISH_MODES
#endif

/****************************************************************************
函数名:static S16 DSM_GetInputType(int32 type)
描  述:将dsm编辑的内容的类型转化成手机识别的类型
参  数:type:dsm 识别的编辑类型
返  回:手机识别的编辑类型
****************************************************************************/
static S32 DSM_GetInputType(int32 type)
{
	S32 edit_input_type;
	
	switch(type)
	{
	case MR_EDIT_ANY:
		edit_input_type=INPUT_TYPE_ALPHANUMERIC_SENTENCECASE;
		break;
	case MR_EDIT_NUMERIC:
		edit_input_type=INPUT_TYPE_PHONE_NUMBER;
		break;
	case MR_EDIT_PASSWORD:
		edit_input_type=INPUT_TYPE_ALPHANUMERIC_PASSWORD|INPUT_TYPE_USE_ONLY_ENGLISH_MODES;
		break;
	case MR_EDIT_ALPHA:
		// 如果客户输入法有问题用下边注释掉的代码试下
		//edit_input_type =IMM_INPUT_TYPE_ENGLISH_SENTENCE|IMM_INPUT_TYPE_NUMERIC;
		edit_input_type =IMM_INPUT_TYPE_ENGLISH_SENTENCE|INPUT_TYPE_USE_ONLY_ENGLISH_MODES;
		break;
	default:
		edit_input_type=INPUT_TYPE_ALPHANUMERIC_SENTENCECASE;
		break;
	}
	return edit_input_type;
}


int32 mr_editCreate(const char * title, const char * text, int32 type, int32 max_size)
{
       T_DSM_EDIT * pEdit = (T_DSM_EDIT *)OslMalloc(sizeof(T_DSM_EDIT));
	int32 len =0;
	int32 slen =0;
	
	if (pEdit != NULL)
	{
	       memset(pEdit,0,sizeof(T_DSM_EDIT));
		pEdit->data = (char *)OslMalloc(DSM_EDIT_BUF_MAX);
		pEdit->pTmp = (char *)OslMalloc(DSM_EDIT_BUF_MAX);
		
		if(pEdit->data != NULL&&pEdit->pTmp != NULL)
		{
		       memset(pEdit->data,0,DSM_EDIT_BUF_MAX);
			memset(pEdit->pTmp,0,DSM_EDIT_BUF_MAX);
			
			pEdit->node.CurrSrcID = dsmAllocScrId();

			if(pEdit->node.CurrSrcID == -1)
			{
				OslMfree(pEdit->data);
				OslMfree(pEdit);
				return MR_FAILED;
			}
			
			pEdit->node.type = DSM_EDIT;
			pEdit->edittype = type;

			if ((max_size <= 0)||(max_size > (DSM_EDIT_BUF_MAX/2-1)))
				pEdit->max = DSM_EDIT_BUF_MAX/2-1;
			else
				pEdit->max = max_size;
			
			dsmCurScrIndex++;
			dsmStack[dsmCurScrIndex] = (T_DSM_NODE *)pEdit;

			slen = mr_str_wstrlen((char *)title);
			len = slen>(DSM_TITLE_LEN-2)?(DSM_TITLE_LEN-2):slen;
	              memcpy(pEdit->node.title,title,len);

			if(text != NULL)
			{
				slen = mr_str_wstrlen((char *)text);
				len = slen>(DSM_EDIT_BUF_MAX-2)?(DSM_EDIT_BUF_MAX-2):slen;	  
		       	memcpy(pEdit->data,text,len);
			}

			mr_str_convert_endian(pEdit->data);
			mr_str_convert_endian(pEdit->node.title);
			
			redraw_edit();
		       return pEdit->node.CurrSrcID;
		}

		if(pEdit->data != NULL)
			OslMfree(pEdit->data);

		if(pEdit->pTmp != NULL)
			OslMfree(pEdit->pTmp);
			
		OslMfree(pEdit);
	}
       return MR_FAILED;
}


/****************************************************************************
函数名:void redraw_edit(void)
描  述:mmi架构调用的显示编辑框的函数
参  数:无
返  回:无
****************************************************************************/
static void redraw_edit(void)
{
      U8* gui_buffer = NULL;
      T_DSM_EDIT *pEdit = (T_DSM_EDIT *)(dsmStack[dsmCurScrIndex]);

	if (pEdit->node.type != DSM_EDIT)
	{
		pEdit = (T_DSM_EDIT*)findNodeByScrnID(mmi_frm_scrn_get_active_id());
		if (pEdit == NULL)
			return;				
	}

      internalCovered = MR_TRUE;
	mr_app_EntryNewScreen(pEdit->node.CurrSrcID, mr_app_exit_dsm_screen, redraw_edit, MMI_FRM_FULL_SCRN);
	mr_backstage_setup_endkey_hdlr();		  
	internalCovered = MR_FALSE;

	 mr_app_resume();

/* this feature add for __MMI_DSM_NEW_JSKY__, later will effect all */
#ifdef __MR_CFG_FEATURE_AUTO_ZOOMIN_ZOOMOUT__
	 mr_layer_zoom_pause_ex();
#endif
	 
	gui_buffer = mmi_frm_scrn_get_gui_buf(mr_app_get_group_id(), pEdit->node.CurrSrcID);	//GetCurrGuiBuffer(pEdit->node.CurrSrcID);
#if defined(__MMI_SKYQQ__) && !defined(WIN32) 
	SKYQQShowIdle(QQ2008_HIDE_ICON);
#endif
	pDsmTitle = (U16 *)pEdit->node.title;
	ShowCategory5Screen_ext(0, IMG_DSM_MAIN_MENU_IMAGE,
                                                             STR_GLOBAL_OK, 0,
                                                             STR_GLOBAL_BACK, 0,
                                                             DSM_GetInputType(pEdit->edittype),
                                                             (U8*) pEdit->data, pEdit->max+1, 
                                                             gui_buffer,0,NULL
                                                             );
	pDsmTitle = NULL;

	//huangsunbo 20110630  解决编辑框界面，需要多按一次OK键才可以返回到应用界面
	SetCenterSoftkeyFunction(mr_eventOk, KEY_EVENT_UP);
	ChangeCenterSoftkey(NULL, IMG_GLOBAL_COMMON_CSK);

      SetLeftSoftkeyFunction(mr_eventOk, KEY_EVENT_UP );
      SetCategory5RightSoftkeyFunction( mr_eventCancel, KEY_EVENT_UP );
}


int32 mr_editRelease(int32 edit)
{
	deleteOneNode(edit);
	return MR_SUCCESS;	
}


const char* mr_editGetText(int32 edit)
{
	T_DSM_EDIT *pEdit = (T_DSM_EDIT *)(dsmStack[dsmCurScrIndex]);

	memset(pEdit->pTmp,0,DSM_EDIT_BUF_MAX);
	memcpy(pEdit->pTmp,MMI_multiline_inputbox.text,mr_str_wstrlen((char *)MMI_multiline_inputbox.text));
	mr_str_convert_endian(pEdit->pTmp);
	
	return (const char*)pEdit->pTmp;
}


/****************************************************************************
函数名:void redraw_win(void)
描  述:为刚调用mr_winCreate创建一个外部窗口，
              注册需要接受的事件
参  数:无
返  回:无
****************************************************************************/
static void redraw_win(void)
{	
	T_DSM_NODE*pWin = (T_DSM_NODE*)(dsmStack[dsmCurScrIndex]);

	if (pWin->type != DSM_WIN)
	{
		pWin = findNodeByScrnID(mmi_frm_scrn_get_active_id());
		if (pWin == NULL)
			return;				
	}

	internalCovered = MR_TRUE;
	mr_app_EntryNewScreen(pWin->CurrSrcID, mr_app_exit_dsm_screen,redraw_win, MMI_FRM_FULL_SCRN);
	internalCovered = MR_FALSE;			

	/*START wuwenjie 2010-11-16*/
	gdi_layer_lock_frame_buffer();
	/*END wuwenjie 2010-11-16*/
	 
	mr_app_setup_dsm_screen_event_hdlr();
	entry_full_screen();
	
	/*START wuwenjie 2010-11-16*/
	gdi_layer_unlock_frame_buffer();
	/*END wuwenjie 2010-11-16*/
	
	mr_app_resume();	
}


int32 mr_winCreate(void)
{
	T_DSM_NODE*pWin = (T_DSM_NODE *)OslMalloc(sizeof(T_DSM_NODE));

	if(pWin != NULL)
	{
		memset(pWin,0,sizeof(T_DSM_NODE));
		pWin->CurrSrcID = dsmAllocScrId();

		if(pWin->CurrSrcID == -1)
		{
		       OslMfree(pWin);
			return MR_FAILED;
		}
		
		pWin->type = DSM_WIN;
		dsmCurScrIndex++;
		dsmStack[dsmCurScrIndex] = (T_DSM_NODE *)pWin;
		redraw_win();
		return pWin->CurrSrcID;
	}
	return MR_FAILED;
}


int32 mr_winRelease(int32 win)
{
	deleteOneNode(win);
	return MR_SUCCESS;
}


void dsm_set_title(U16 **pTitle)
{
	if(pTitle&&pDsmTitle)
	{
		*pTitle = pDsmTitle;
	}
}



int32 mr_localui_menu_create_ex(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	T_LISTBOXWIN *listwin = (T_LISTBOXWIN*)input;
	return mr_menuCreateEx(listwin->title, listwin->itemNum, listwin->listType);
}


int32 mr_localui_menu_set_menuitem_ex(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	T_LISTBOXITEM *listitem = (T_LISTBOXITEM*)input;
	return mr_menuSetItemEx(listitem->menu, listitem->text, listitem->index, listitem->selected);
}


int32 mr_localui_menu_set_focus(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	T_LISTBOXFOCUS * pfocus = (T_LISTBOXFOCUS *)input;
	if(!pfocus || input_len != sizeof(T_LISTBOXFOCUS))
		return MR_FAILED;
	
	return mr_menuSetFocus(pfocus->menu, pfocus->hilite);
}

#endif

